#include "dx8_avi.h"

#include <d3dx8.h>
#include <cassert>

#include "graphics/graphics.h"
#include "graphics/graphics_util.h"
#include "dx8graphics/dx8_graphics.h"


#include "util/timer.h"		    
#include "util/log.h"		    // log
#include "util/common_macros.h"		// macros

#include "vfs/vfs.h"

#include "util/mmgr.h"	

Dx8AVI::Dx8AVI() :
	AVI(),
	m_pTexture(NULL)
{

}

Dx8AVI::~Dx8AVI()
{
	UTIL_SAFE_RELEASE(m_pTexture);
}

bool    Dx8AVI::SetTextureStage(unsigned int _TexStage)
{
    if (m_pTexture)
       ((Dx8Graphics*)GetGraphics())->GetDevice()->SetTexture(_TexStage, m_pTexture);

    return true;
}


void	Dx8AVI::BlitToTexture()
{
	assert(m_ResizeDataA && "Invalid data for AVI");
	//assert(m_pTexture && "No valid texture for AVI");
	if (!m_pTexture)
	{
		_LOG(MSG_INIT, "  AVI: Initializing texture");
		InitTexture();
		_LOG(MSG_INIT, "  AVI: Texture initialized");
	}
    D3DLOCKED_RECT LockedRegion;
    HRESULT hRes = m_pTexture->LockRect(0, &LockedRegion, NULL, D3DLOCK_DISCARD);
//        assert(SUCCEEDED(hRes) && "Failed to lock texture for AVI file");
	if (SUCCEEDED(hRes))
	{
		//_LOG(MSG_INIT, "  AVI: Copying data to locked region [" << LockedRegion.Pitch << " x " << " Height");

		GraphicsUtil::CopyBufferRect(LockedRegion.pBits, m_ResizeDataA, m_Width, m_Height, LockedRegion.Pitch);
		/*
		if (LockedRegion.Pitch == (m_Width * 4))
		{
			// good - we can do a safe memcpy
			memcpy(LockedRegion.pBits, m_ResizeDataA, LockedRegion.Pitch*m_Height);
		}
		else
		{
			// horrible memcpy code coming right up!
			unsigned char* pbyBuffer = (unsigned char*)LockedRegion.pBits;
			int idxBuffer, idxData;
			for (int i = 0; i < m_Height; i++)
			{
				idxBuffer = i * LockedRegion.Pitch;
				idxData = i * m_Width * 4;
				memcpy(&pbyBuffer[idxBuffer], &m_ResizeDataA[idxData], m_Width * 4);
			}
		}
		*/

		//_LOG(MSG_INIT, "  AVI: Unlockign rect");
		m_pTexture->UnlockRect(0);
		//_LOG(MSG_INIT, "  AVI: Rect unlocked");
	}
}

bool Dx8AVI::InitTexture()
{
  	UTIL_SAFE_RELEASE(m_pTexture);
  // Create D3D8 Texture
	HRESULT hRes =
	D3DXCreateTexture(  ((Dx8Graphics*)GetGraphics())->GetDevice(),		    // device
                        m_Width,            // width
					    m_Height,           // height
					    1,                  // Mipmap levels
					    D3DUSAGE_DYNAMIC,   // it's a dynamic surface
					    D3DFMT_R8G8B8,      // 24 bit RGB format
					    D3DPOOL_DEFAULT,    // default pool, it'll be allocated in VRAM
					    &m_pTexture); // texture
    if (FAILED(hRes))
    {
//        assert(0 && "Failed to create a texture for the AVI file");
        _LOG(MSG_ERROR, "Unable to create a " << m_Width << " x " << m_Height << " texture surface for the AVI file");
        // shutdown
        DeleteObject(m_hBitmap);								// Delete The Device Dependant Bitmap Object
	    DrawDibClose(m_hDrawDIB);								// Closes The DrawDib Device Context
	    AVIStreamGetFrameClose(m_pGetFrame);					// Deallocates The GetFrame Resources
	    AVIStreamRelease(m_pAVIStream);							// Release The Stream
	    AVIFileExit();											// Release The File
        return false;
    }
	return true;
}

bool	Dx8AVI::ShutdownTexture()
{
	UTIL_SAFE_RELEASE(m_pTexture);	
	return true;
}
